package com.parking.carsystem.parkingchargesystem.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.parking.carsystem.parkingchargesystem.R;
import com.parking.carsystem.parkingchargesystem.adapter.CouponCustomPagerAdapter;
import com.parking.carsystem.parkingchargesystem.adapter.CouponViewpagerAdapter;
import com.parking.carsystem.parkingchargesystem.common.Constant;
import com.parking.carsystem.parkingchargesystem.fragment.CouponNotUseFragment;
import com.parking.carsystem.parkingchargesystem.fragment.FragmentFactory;
import com.parking.carsystem.parkingchargesystem.module.CouponTypeModel;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CouponActivity extends BaseActivity implements ViewPager.OnPageChangeListener, CouponViewpagerAdapter.OnPageItemClickListener, View.OnClickListener {
    @BindView(R.id.view_page_coupon)
    ViewPager viewPageCoupon;
    @BindView(R.id.table_layout)
    TabLayout tableLayout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.setting)
    TextView setting;
    @BindView(R.id.title_bg)
    RelativeLayout titleBg;
    @BindView(R.id.not_user)
    TextView notUser;
    @BindView(R.id.not_user_line)
    View notUserLine;
    @BindView(R.id.userd)
    TextView userd;
    @BindView(R.id.userd_line)
    View userdLine;
    @BindView(R.id.invalid)
    TextView invalid;
    @BindView(R.id.invalid_line)
    View invalidLine;
    @BindView(R.id.layout_not_use)
    RelativeLayout layoutNotUse;
    @BindView(R.id.layout_use)
    RelativeLayout layoutUse;
    @BindView(R.id.layout_invalid)
    RelativeLayout layoutIncalid;
    private ArrayList<String> titliText;
    private long delayMillis = 4000;
    public int START_VIEW = 1;
    public int STOP_VIEW = 2;
    private CouponViewpagerAdapter viewpagerAdapter;
    private CouponCustomPagerAdapter couponCustomPagerAdapter;
    private ArrayList<CouponTypeModel.RowsBean> couponRows;
    private int parkinglotId;
    private String plateNumber;
    private CouponNotUseFragment couponFragmentInsance;


    @Override
    protected int getContentViewId() {
        return R.layout.acitivity_coupon;
    }

    @Override
    public void initView() {
        text.setText("优惠卷");
        back.setVisibility(View.VISIBLE);
        plateNumber = getIntent().getStringExtra(Constant.PLATENUMBER);
//        if (titliText == null) {
//            titliText = new ArrayList<>();
//            titliText.add(getResources().getString(R.string.notused));
//            titliText.add(getResources().getString(R.string.yesused));
//            titliText.add(getResources().getString(R.string.failure));
//        }
        viewpagerAdapter = new CouponViewpagerAdapter();
        viewpagerAdapter.setItemClickListener(this);
        viewPageCoupon.addOnPageChangeListener(this);
        if (couponCustomPagerAdapter == null) {
            couponCustomPagerAdapter = new CouponCustomPagerAdapter(getSupportFragmentManager()/*, titliText*/);
        }
        if (viewpager != null) {
            viewpager.setAdapter(couponCustomPagerAdapter);
        }
        //通过tablelayout设置表头
//        tableLayout.setupWithViewPager(viewpager);
        couponFragmentInsance = (CouponNotUseFragment) FragmentFactory.getCouponFragmentInsance(0);
        if (couponFragmentInsance != null) {
            couponFragmentInsance.setPlateNumber(plateNumber);
        }
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                changeData(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewpager.setOffscreenPageLimit(2);
        layoutNotUse.setOnClickListener(this);
        layoutUse.setOnClickListener(this);
        layoutIncalid.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        couponFragmentInsance.initData();
    }

    private void changeData(int position) {
        if (position == 0) {
            //未使用
            changeTextLineBlack(notUser, notUserLine, getResources().getColor(R.color.main_color));
            changeTextLineGray(userd, userdLine, getResources().getColor(R.color.color_b8b8b8));
            changeTextLineGray(invalid, invalidLine, getResources().getColor(R.color.color_b8b8b8));
        } else if (position == 1) {
            //已使用
            changeTextLineGray(notUser, notUserLine, getResources().getColor(R.color.color_b8b8b8));
            changeTextLineBlack(userd, userdLine, getResources().getColor(R.color.main_color));
            changeTextLineGray(invalid, invalidLine, getResources().getColor(R.color.color_b8b8b8));
        } else if (position == 2) {
            //已失效
            changeTextLineGray(notUser, notUserLine, getResources().getColor(R.color.color_b8b8b8));
            changeTextLineGray(userd, userdLine, getResources().getColor(R.color.color_b8b8b8));
            changeTextLineBlack(invalid, invalidLine, getResources().getColor(R.color.main_color));
        }
    }

    private void changeTextLineBlack(TextView textView, View view, int color) {
        textView.setTextColor(color);
        view.setVisibility(View.VISIBLE);
    }

    private void changeTextLineGray(TextView textView, View view, int color) {
        textView.setTextColor(color);
        view.setVisibility(View.GONE);
    }

    @Override
    public void initData() {
        getCouponMessage();
    }

    /**
     * 获取优惠券的信息
     */
    private void getCouponMessage() {
        OkGo.<String>get(Constant.url + Constant.LISTALLCOUPON)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        CouponTypeModel couponTypeModel = gson.fromJson(response.body(), CouponTypeModel.class);
                        if (couponTypeModel.code == 200 && couponTypeModel.rows.size() > 0) {
                            couponRows = couponTypeModel.rows;
                            viewpagerAdapter.setArrayList(couponRows, CouponActivity.this);
                            viewPageCoupon.setCurrentItem(Integer.MAX_VALUE / 2 - (Integer.MAX_VALUE / 2 % couponRows.size()));
                            viewPageCoupon.setAdapter(viewpagerAdapter);
                            if (couponRows.size() > 1) {
                                handler.sendEmptyMessageDelayed(START_VIEW, delayMillis);
                            }
                        } else {
                            viewPageCoupon.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        viewPageCoupon.setVisibility(View.GONE);
                    }
                });
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        switch (state) {
            //用户滑动时停止轮播
            case ViewPager.SCROLL_STATE_DRAGGING:
                handler.sendEmptyMessage(STOP_VIEW);
                break;
            //用户滑动完成时继续轮播
            case ViewPager.SCROLL_STATE_IDLE:
                handler.sendEmptyMessageDelayed(START_VIEW, delayMillis);
                break;
        }
    }

    @Override
    public void handleMessageMeathed(Message msg) {
        switch (msg.what) {
            case 1:
                viewPageCoupon.setCurrentItem(viewPageCoupon.getCurrentItem() + 1);
                break;
            case 2:
                handler.removeCallbacksAndMessages(null);
                break;
        }
    }

    @Override
    public void onItemClickLisener(int id) {
        //跳转到购买的优惠券的页面
        Intent intent = new Intent(this, SaveMoneyRollsActivity.class);
        intent.putExtra(Constant.COUPONID, id);
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_use:
                changeData(1);
                break;
            case R.id.layout_not_use:
                changeData(0);
                break;
            case R.id.layout_invalid:
                changeData(2);
                break;
        }
    }

    public void setNotSize(int size) {
        notUser.setText(size == 0 ? "未使用" : "未使用(" + size + ")");
    }

    public void setFailureText(int size) {
        userd.setText(size == 0 ? "已失效" : "已失效(" + size + ")");
    }

    public void setAlreadyText(int size) {
        invalid.setText(size == 0 ? "已使用" : "已使用(" + size + ")");
    }
}
